<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Notification;
use Illuminate\Http\Request;

class NotificationController extends Controller
{
    /**
     * 获取用户通知列表
     */
    public function index(Request $request)
    {
        $user = $request->user();

        $query = Notification::where('user_id', $user->id)
            ->with('fromUser');

        // 筛选类型
        if ($request->has('type')) {
            $query->where('type', $request->type);
        }

        // 筛选状态
        if ($request->has('is_read')) {
            $query->where('is_read', $request->boolean('is_read'));
        }

        $notifications = $query->orderBy('created_at', 'desc')
            ->paginate($request->get('per_page', 20));

        return response()->json([
            'notifications' => $notifications->items(),
            'pagination' => [
                'total' => $notifications->total(),
                'current_page' => $notifications->currentPage(),
                'per_page' => $notifications->perPage(),
                'last_page' => $notifications->lastPage(),
            ],
            'unread_count' => Notification::where('user_id', $user->id)
                ->where('is_read', false)
                ->count(),
        ]);
    }

    /**
     * 获取未读通知数量
     */
    public function unreadCount(Request $request)
    {
        $user = $request->user();

        $count = Notification::where('user_id', $user->id)
            ->where('is_read', false)
            ->count();

        return response()->json(['count' => $count]);
    }

    /**
     * 标记通知为已读
     */
    public function markAsRead(Request $request, Notification $notification)
    {
        // 确保通知属于当前用户
        if ($notification->user_id !== $request->user()->id) {
            return response()->json(['message' => '无权操作'], 403);
        }

        $notification->markAsRead();

        return response()->json([
            'message' => '标记成功',
            'notification' => $notification,
        ]);
    }

    /**
     * 标记所有通知为已读
     */
    public function markAllAsRead(Request $request)
    {
        $user = $request->user();

        Notification::where('user_id', $user->id)
            ->where('is_read', false)
            ->update([
                'is_read' => true,
                'read_at' => now(),
            ]);

        return response()->json(['message' => '已全部标记为已读']);
    }

    /**
     * 删除通知
     */
    public function destroy(Request $request, Notification $notification)
    {
        // 确保通知属于当前用户
        if ($notification->user_id !== $request->user()->id) {
            return response()->json(['message' => '无权操作'], 403);
        }

        $notification->delete();

        return response()->json(['message' => '删除成功']);
    }

    /**
     * 批量删除通知
     */
    public function destroyBatch(Request $request)
    {
        $request->validate([
            'notification_ids' => 'required|array',
            'notification_ids.*' => 'exists:notifications,id',
        ]);

        $user = $request->user();

        Notification::where('user_id', $user->id)
            ->whereIn('id', $request->notification_ids)
            ->delete();

        return response()->json(['message' => '批量删除成功']);
    }

    /**
     * 清空所有通知
     */
    public function clear(Request $request)
    {
        $user = $request->user();

        // 可以选择只清空已读通知
        $query = Notification::where('user_id', $user->id);

        if ($request->boolean('read_only')) {
            $query->where('is_read', true);
        }

        $count = $query->delete();

        return response()->json([
            'message' => "已清空 {$count} 条通知",
            'count' => $count,
        ]);
    }

    /**
     * 获取通知设置
     */
    public function settings(Request $request)
    {
        $user = $request->user();

        // 这里可以返回用户的通知设置
        // 例如：哪些类型的通知要接收，是否接收邮件通知等
        $settings = [
            'email_notifications' => $user->email_notifications ?? true,
            'follow_notifications' => $user->follow_notifications ?? true,
            'comment_notifications' => $user->comment_notifications ?? true,
            'like_notifications' => $user->like_notifications ?? true,
            'system_notifications' => $user->system_notifications ?? true,
        ];

        return response()->json(['settings' => $settings]);
    }

    /**
     * 更新通知设置
     */
    public function updateSettings(Request $request)
    {
        $request->validate([
            'email_notifications' => 'boolean',
            'follow_notifications' => 'boolean',
            'comment_notifications' => 'boolean',
            'like_notifications' => 'boolean',
            'system_notifications' => 'boolean',
        ]);

        $user = $request->user();

        // 更新用户的通知设置
        // 这里假设用户模型有这些字段，如果没有需要添加
        $user->update($request->only([
            'email_notifications',
            'follow_notifications',
            'comment_notifications',
            'like_notifications',
            'system_notifications',
        ]));

        return response()->json([
            'message' => '设置已更新',
            'settings' => $request->all(),
        ]);
    }

    /**
     * 获取未读通知数量
     */
    public function unreadCount(Request $request)
    {
        $user = $request->user();

        $count = Notification::where('user_id', $user->id)
            ->where('is_read', false)
            ->count();

        return response()->json(['count' => $count]);
    }
}